package uutalk.netcloud.talk.app.ui.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.LinearLayout;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.Constants;
import uutalk.netcloud.talk.app.ui.MainActivity;
import uutalk.netcloud.talk.base.android.BaseActivity;
import uutalk.netcloud.talk.base.mqtt.MQTTManager;
import uutalk.netcloud.talk.base.util.CommonUtil;
import uutalk.netcloud.talk.base.util.LanguageUtil;
import uutalk.netcloud.talk.base.util.SharedPreferencesUtil;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;

/**
 * 类描述：
 * 作者：Pengmutian
 * 时间：2018/4/17
 */
public class ChooseLanuageActivity  extends BaseActivity{

    private final int CHECK_SIM = 1000;

    @BindView(R.id.llChooseLangue)
    LinearLayout llChooseLangue;
    @BindView(R.id.llInit)
    LinearLayout llInit;
    @BindView(R.id.llSim)
    LinearLayout llSim;

    @Override
    public int layoutId() {
        return R.layout.lz_activity_choose_lanuage;
    }

    @Override
    public boolean supportActionbar() {
        return false;
    }

    @Override
    public void init() {

    }

    @OnClick({R.id.tvEnglish, R.id.tvChina, R.id.btnSure, R.id.btnCancel})
    public void onViewClick(View view){
        switch (view.getId()){
            case R.id.tvEnglish:{
                llChooseLangue.setVisibility(View.GONE);
                llInit.setVisibility(View.VISIBLE);
                LanguageUtil.changeLanguage(2);
                sendMessageToPageDelay(CHECK_SIM, 2000l);
            } break;
            case R.id.tvChina:{
                llChooseLangue.setVisibility(View.GONE);
                llInit.setVisibility(View.VISIBLE);
                LanguageUtil.changeLanguage(1);
                sendMessageToPageDelay(CHECK_SIM, 2000l);
            } break;
            case R.id.btnSure:{
                enterNextActivity();
            } break;
            case R.id.btnCancel:{
                finish();
            } break;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if(msg.what == CHECK_SIM){
            if(CommonUtil.hasSimCard(mActivity)){
                enterNextActivity();
            } else {
                llInit.setVisibility(View.GONE);
                llSim.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 进入下一个页面
     */
    private void enterNextActivity(){
        SharedPreferencesUtil.setInt(mActivity, Constants.FIRST_LAUNCH, 1);//第一次登陆初始化完成
        if (UserManager.getInstance().getLoginInfo() != null) {
            MQTTManager.getInstance().connect(UserManager.getInstance().getLoginInfo().getDeviceId());
            Intent intent = new Intent(mActivity, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(mActivity, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

}
